package com.cjack.frenchexcel.controller;

import com.cjack.frenchexcel.config.AjaxResult;
import com.cjack.frenchexcel.exception.BankCardExsitException;
import com.cjack.frenchexcel.exception.BankCardNotExsitException;
import com.cjack.frenchexcel.model.MooncardCardModel;
import com.cjack.frenchexcel.service.MooncardCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/mooncardCard")
public class MooncardCardEndpoint extends BaseEndpoint{

    @Autowired
    MooncardCardService service;

    /**
     * 列表
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult list( @RequestParam("adminUserId") Long adminUserId) {
        List<MooncardCardModel> datas = service.list( adminUserId);
        return AjaxResult.SUCCESS( datas);
    }

    /**
     * 新增
     * @return
     */
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult addOrUpdate( MooncardCardModel model) {
        try{
            if( isAdd( model.getId())){
                model.setCreateTime( new Date());
            }
            service.save( model);
            return AjaxResult.SUCCESS();
        } catch (BankCardExsitException e) {
            return AjaxResult.ERROR( e.getCode(), e.getMessage());
        } catch (BankCardNotExsitException e) {
            return AjaxResult.ERROR( e.getCode(), e.getMessage());
        }
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult del( @RequestParam("id") Long id) {
        try{
            service.del( id);
            return AjaxResult.SUCCESS( "删除成功");
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "/delByBankNo", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult delByBankNo( @RequestParam("bankNo") String bankNo) {
        try{
            service.delByBankNo( bankNo);
            return AjaxResult.SUCCESS( "删除成功");
        }catch (BankCardNotExsitException e) {
            return AjaxResult.ERROR( e.getCode(), e.getMessage());
        }
    }
}
